<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格编号" prop="sizeId">
        <el-input
          v-model="queryParams.sizeId"
          placeholder="请输入规格编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌编号" prop="brandId">
        <el-input
          v-model="queryParams.brandId"
          placeholder="请输入品牌编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="零售价" prop="productPrice">
        <el-input
          v-model="queryParams.productPrice"
          placeholder="请输入零售价"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="productUnit">
        <el-input
          v-model="queryParams.productUnit"
          placeholder="请输入单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批发价" prop="purchasePrice">
        <el-input
          v-model="queryParams.purchasePrice"
          placeholder="请输入批发价"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图片地址" prop="productImg">
        <el-input
          v-model="queryParams.productImg"
          placeholder="请输入图片地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="typeId">
        <el-input
          v-model="queryParams.typeId"
          placeholder="请输入类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="删除" prop="isDel">
        <el-input
          v-model="queryParams.isDel"
          placeholder="请输入删除"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="权重" prop="selectCount">
        <el-input
          v-model="queryParams.selectCount"
          placeholder="请输入权重"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存" prop="productStore">
        <el-input
          v-model="queryParams.productStore"
          placeholder="请输入库存"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="简称" prop="productShortName">
        <el-input
          v-model="queryParams.productShortName"
          placeholder="请输入简称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['product:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品编号" align="center" prop="productId" />
      <el-table-column label="商品名" align="center" prop="productName" />
      <el-table-column label="规格编号" align="center" prop="sizeId" />
      <el-table-column label="品牌编号" align="center" prop="brandId" />
      <el-table-column label="零售价" align="center" prop="productPrice" />
      <el-table-column label="单位" align="center" prop="productUnit" />
      <el-table-column label="批发价" align="center" prop="purchasePrice" />
      <el-table-column label="图片地址" align="center" prop="productImg" />
      <el-table-column label="类型" align="center" prop="typeId" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="删除" align="center" prop="isDel" />
      <el-table-column label="权重" align="center" prop="selectCount" />
      <el-table-column label="库存" align="center" prop="productStore" />
      <el-table-column label="简称" align="center" prop="productShortName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入商品名" />
        </el-form-item>
        <el-form-item label="规格编号" prop="sizeId">
          <el-input v-model="form.sizeId" placeholder="请输入规格编号" />
        </el-form-item>
        <el-form-item label="品牌编号" prop="brandId">
          <el-input v-model="form.brandId" placeholder="请输入品牌编号" />
        </el-form-item>
        <el-form-item label="零售价" prop="productPrice">
          <el-input v-model="form.productPrice" placeholder="请输入零售价" />
        </el-form-item>
        <el-form-item label="单位" prop="productUnit">
          <el-input v-model="form.productUnit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="批发价" prop="purchasePrice">
          <el-input v-model="form.purchasePrice" placeholder="请输入批发价" />
        </el-form-item>
        <el-form-item label="图片地址" prop="productImg">
          <el-input v-model="form.productImg" placeholder="请输入图片地址" />
        </el-form-item>
        <el-form-item label="类型" prop="typeId">
          <el-input v-model="form.typeId" placeholder="请输入类型" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="删除" prop="isDel">
          <el-input v-model="form.isDel" placeholder="请输入删除" />
        </el-form-item>
        <el-form-item label="权重" prop="selectCount">
          <el-input v-model="form.selectCount" placeholder="请输入权重" />
        </el-form-item>
        <el-form-item label="库存" prop="productStore">
          <el-input v-model="form.productStore" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="简称" prop="productShortName">
          <el-input v-model="form.productShortName" placeholder="请输入简称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, exportInfo } from "@/api/product/info";

export default {
  name: "Info",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        sizeId: null,
        brandId: null,
        productPrice: null,
        productUnit: null,
        purchasePrice: null,
        productImg: null,
        typeId: null,
        isDel: null,
        selectCount: null,
        productStore: null,
        productShortName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productName: [
          { required: true, message: "商品名不能为空", trigger: "blur" }
        ],
        productPrice: [
          { required: true, message: "零售价不能为空", trigger: "blur" }
        ],
        productUnit: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ],
        purchasePrice: [
          { required: true, message: "批发价不能为空", trigger: "blur" }
        ],
        productImg: [
          { required: true, message: "图片地址不能为空", trigger: "blur" }
        ],
        typeId: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
        isDel: [
          { required: true, message: "删除不能为空", trigger: "blur" }
        ],
        selectCount: [
          { required: true, message: "权重不能为空", trigger: "blur" }
        ],
        productStore: [
          { required: true, message: "库存不能为空", trigger: "blur" }
        ],
        productShortName: [
          { required: true, message: "简称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productId: null,
        productName: null,
        sizeId: null,
        brandId: null,
        productPrice: null,
        productUnit: null,
        purchasePrice: null,
        productImg: null,
        typeId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        isDel: null,
        selectCount: null,
        productStore: null,
        productShortName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.productId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const productId = row.productId || this.ids
      getInfo(productId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.productId != null) {
            updateInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const productIds = row.productId || this.ids;
      this.$confirm('是否确认删除商品信息编号为"' + productIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfo(productIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有商品信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
